import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import {Message} from 'element-ui'
import 'bootstrap/dist/css/bootstrap.min.css'
import 'bootstrap/dist/js/bootstrap.min.js'
import axios from "axios";
import jquery from 'jquery';
import qs from 'qs'


Vue.use(ElementUI)
Vue.prototype.$ = jquery
Vue.config.productionTip = false
Vue.prototype.$axios = axios
Vue.prototype.qs=qs

new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')

//用qs解析，jsonToForm
// 解决post请求400
axios.interceptors.request.use(config => {
    if(config.type == 'formData' || config.method != 'post'){
        return config
    }
    config.data = qs.stringify(config.data)
    return config
}, (err) =>{
    Message.error({
        message: '请求超时！'
    });
    return Promise.reject(err);
})

// axios.interceptors.request.use(config => {
//     return config;
// }, err => {
//     Message.error({message: '请求超时!'});
// })
axios.interceptors.response.use(data => {
    if (data.status && data.status === 200 && data.data.status === 500) {
        Message.error({message: data.data.msg});
        return;
    }
    if (data.data.msg) {
        // Message.success({message: data.data.msg});
        return data.data;
    }

}, err => {
    if (err.response.status === 504 || err.response.status === 404) {
        Message.error({message: '服务器被吃了⊙﹏⊙∥'});
    } else if (err.response.status == 403) {
        Message.error({message: '权限不足,请联系管理员!'});
    } else if (err.response.status == 401) {
        Message.error({message: err.response.data.msg});
    } else {
        if (err.response.data.msg) {
            Message.error({message: err.response.data.msg});
        } else {
            Message.error({message: '未知错误!'});
        }
    }
})
// Vue.prototype.getCookie=function (cname){
//     var name = cname + "=";
//     var ca = document.cookie.split(';');
//     console.log("获取cookie,现在循环")
//     for (var i = 0; i < ca.length; i++) {
//         var c = ca[i];
//         console.log(c)
//         while (c.charAt(0) == ' ') c = c.substring(1);
//         if (c.indexOf(name) != -1){
//             return c.substring(name.length, c.length);
//         }
//     }
//     return "";
// }


